package com.yewen.http.controller;

import com.yewen.constants.ServiceExceptionEnum;
import com.yewen.http.core.vo.CommonResult;
import com.yewen.http.entity.Account;
import com.yewen.http.entity.EducationCounselor;
import com.yewen.http.entity.LoginAccount;
import com.yewen.http.service.AccountService;
import com.yewen.http.service.EducationCounselorService;
import com.yewen.util.HMACEncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/yewen/account")
public class AccountHandler {
    @Autowired
    private AccountService accountService;
    @Autowired
    private EducationCounselorService educationCounselorService;

    @PostMapping("/studentRegister")
    public CommonResult studentRegister(HttpSession session,Account account) {
        /*用户已经存在*/
        if (accountService.findAccountById(account.getId()) != null) {
            return CommonResult.error(ServiceExceptionEnum.USER_EXISTED);
        }else if (accountService.findAccountByTel(account.getTel())!=null){
            return CommonResult.error(ServiceExceptionEnum.TEL_EXISTED);
        }else if(accountService.findAccountByEmail(account.getEmail())!=null){
            return CommonResult.error(ServiceExceptionEnum.EMAIL_EXISTED);
        } else {
            if (accountService.save(account)) {
                session.setAttribute("isLogin","true");
                return CommonResult.success("注册成功");
            }
            else return CommonResult.error(ServiceExceptionEnum.SERVER_BUSY);
        }
    }

    @PostMapping("/educounselorRegister")
    public CommonResult educounselorRegister(HttpSession session,EducationCounselor educationCounselor){
        if (educationCounselor!=null){
            if (educationCounselorService.checkTelExisted(educationCounselor.getTel())){
                return CommonResult.error(ServiceExceptionEnum.TEL_EXISTED);
            }
            if ((educationCounselorService.checkEmailExisted(educationCounselor.getEmail()))){
                return CommonResult.error(ServiceExceptionEnum.EMAIL_EXISTED);
            }
            educationCounselorService.register(educationCounselor);
            session.setAttribute("isLogin","true");
            return CommonResult.success("注册成功!");
        }
        return CommonResult.error(ServiceExceptionEnum.UNKNOWN_ERROR);
    }
    /**
     * 登陆验证,并把token传回前端
     * @return token
     * @if 登陆成功 把token传回前端保存
     * @else 密码或用户名错误，提示用户。
     */
    @PostMapping("/loginAuth")
    public CommonResult loginAuth(HttpServletRequest request,String id, String password) {
        /*连接数据库查询*/
        if (accountService.loginAuth(new LoginAccount(id, password))) {
            /*生成token附在data中返回给客户端*/
            String token = HMACEncryptUtil.generateToken(id, true);
            if (token==null){
                return CommonResult.error(ServiceExceptionEnum.GEN_TOKEN_FAIL);
            }else{
                request.getSession().setAttribute("isLogin",true);
                return CommonResult.success(token);
            }
        } else if (!accountService.loginAuth(new LoginAccount(id, password))) {
            return CommonResult.error(ServiceExceptionEnum.ID_OR_PASSWORD_WRONG);
        } else {
            return CommonResult.error(ServiceExceptionEnum.UNKNOWN_ERROR);
        }
    }

}
